package com.east.service.statistical.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.east.bean.Contract;
import com.east.bean.Fee;
import com.east.bean.OtherFee;
import com.east.bean.Project;
import com.east.controller.staff.echar.StaffEchar;
import com.east.controller.statistical.Operation;
import com.east.controller.statistical.ProjectCost;
import com.east.dao.ContractMapper;
import com.east.dao.FeeMapper;
import com.east.dao.IncomeMapper;
import com.east.dao.OtherFeeMapper;
import com.east.dao.ProjectMapper;
import com.east.dao.ReimburseRecordsMapper;
import com.east.service.statistical.ProjectCostService;
import com.east.utils.DateUtils;
import com.east.utils.Pagenation;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
@Service("projectCostService")
public class ProjectCostServiceImpl implements ProjectCostService{
	@Autowired
	ProjectMapper projectMapper;
	@Autowired
	FeeMapper feeMapper;
	@Autowired
	OtherFeeMapper otherFeeMapper;
	@Autowired
	ReimburseRecordsMapper reimburseRecordsMapper;
	@Autowired
	ContractMapper contractMapper;
	@Autowired
	IncomeMapper incomeMapper;
	
	@Override
	public Page<ProjectCost> findList(Pagenation pagenation, String key,String startTime ,String endTime) {
		Page<ProjectCost> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());
		if(StringUtils.isNotBlank(startTime)) {
			startTime="WXKJXM"+startTime.replace("-", "");
		}
		if(StringUtils.isNotBlank(endTime)) {
			endTime="WXKJXM"+endTime.replace("-", "");
		}else {
			String daysOfDate = DateUtils.getDaysOfDate(new Date());
			endTime="WXKJXM"+daysOfDate.replace("-", "");
		}
		projectMapper.findByProjectCost(key,startTime,endTime);
		return startPage;
	}

	@Override
	public ProjectCost findOne(Integer id) {
		return projectMapper.findByProjectCostOne(id);
	}

	@Override
	public List<Contract> findByContract(Integer id) {
		return contractMapper.findByContract(id);
	}

	@Override
	public List<Fee> findByFee(Integer id) {
		return feeMapper.findByProjectCost(id);
	}

	@Override
	public List<OtherFee> findByOtherFee(Integer id) {
		return otherFeeMapper.findByProjectCost(id);
	}

	@Override
	public Map<String, Object> findOperation(Integer year) {
		Map<String, Object> mapList=new HashMap<String,Object>();
		Map<String, Object> map=new HashMap<String,Object>();
		map.put("type", 26);
		map.put("serNum", "WXKJXM"+year);
		/*Map<String, Object> maps=new HashMap<String,Object>();
		maps.put("type", 27);
		maps.put("serNum", "WXKJXM"+year);*/
		Map<String, Object> mapCg=new HashMap<String,Object>();
		mapCg.put("type", 27);
		mapCg.put("serNum", "WXKJ"+year);
		
		Operation findByOperation = projectMapper.findByOperation(map);//项目销售成本
		Operation findByOperation2 = projectMapper.findByOperation(mapCg);//项目采购成本
		Operation findByOperation7 = contractMapper.findByOperation(map);//实际销售成本
		Operation findByOperation8 = contractMapper.findByOperation(mapCg);//实际采购成本
		List<Operation> findByOperation6 = contractMapper.findByOperationChart(map);//年度总金额
		Double findByOperation3 = feeMapper.findByOperation(year);//年项目费用
		Double findByOperation4 = otherFeeMapper.findByOperation(year);//其他费用
		Double findByOperation5 = reimburseRecordsMapper.findByOperation(year);//管理费用
		
		Double otherC=formatDouble1(findByOperation3+findByOperation4+findByOperation5);//其他成本
		
		
		Double findByOperation9 = incomeMapper.findByOperation(year);
		
		
		//年项目成本
		Double projectX=formatDouble1(otherC+Double.valueOf(findByOperation8.getAcount()));
		//毛利润
		Double Mao=formatDouble1((Double.valueOf(findByOperation7.getAcount())-projectX));
		//利润率
		double maoL= formatDouble1((Mao/Double.valueOf(findByOperation7.getAcount()))*100);
		List<String> type=Lists.newArrayList();
		
		List<Object> acount=Lists.newArrayList();
		for (Operation operation : findByOperation6) {
			type.add(StringUtils.isNotBlank(operation.getName())?operation.getName():"其他");
			Map<String, Object> mapO=new HashMap<String,Object>();
			mapO.put("name",StringUtils.isNotBlank(operation.getName())?operation.getName():"其他");
			mapO.put("value", StringUtils.isNotBlank(operation.getAcount())?operation.getAcount():"0.00");
			acount.add(mapO);
		}
		
		List<Operation> findBySaleChart = contractMapper.findBySaleChart(map);
		String[] list= {"01","02","03","04","05","06","07","08","09","10","11","12"};
		String[] listName= {"0","0","0","0","0","0","0","0","0","0","0","0"};
		for (Operation o : findBySaleChart) {
			if(!o.getName().equals("00")) {
							String substring = o.getName().substring(0, 1);
							if(Integer.valueOf(substring)>0) {
								listName[Integer.valueOf(o.getName())-1]=o.getAcount();
							}else{
								listName[Integer.valueOf(o.getName().substring(1, 2))-1]=o.getAcount();
							}
							
			}
		}
		List<String> sjzc=Lists.newArrayList();//年实际支出
		sjzc.add("采购成本");
		sjzc.add("项目费用");
		sjzc.add("其他费用");
		sjzc.add("管理费用");
		List<Object> sjzcV=Lists.newArrayList();
		Map<String, Object> map1=new HashMap<String,Object>();
		map1.put("name", "采购成本");
		map1.put("value", findByOperation8.getAcount());
		sjzcV.add(map1);
		Map<String, Object> map2=new HashMap<String,Object>();
		map2.put("name", "项目费用");
		map2.put("value", findByOperation3.toString());
		sjzcV.add(map2);
		Map<String, Object> map3=new HashMap<String,Object>();
		map3.put("name", "其他费用");
		map3.put("value", findByOperation4.toString());
		sjzcV.add(map3);
		Map<String, Object> map4=new HashMap<String,Object>();
		map4.put("name", "管理费用");
		map4.put("value", findByOperation5.toString());
		sjzcV.add(map4);
		
		
		//实际收入
		Double sjsr=findByOperation9+Double.valueOf(findByOperation7.getAcount());
		mapList.put("sjsr", sjsr);//实际收入
		mapList.put("sjlr", formatDouble1(sjsr-projectX));//实际利润
		mapList.put("sjlv", formatDouble1(((sjsr-projectX)/sjsr)*100));//实际利润率
		
		//销售统计
		mapList.put("list", list);
		mapList.put("listName", listName);
		
		mapList.put("projectX", findByOperation);
		mapList.put("projectC", findByOperation2);
		mapList.put("contractX", findByOperation7);
		mapList.put("contractC", findByOperation8);
		mapList.put("fee", findByOperation3);
		mapList.put("otherFee", findByOperation4);
		mapList.put("reimburse", findByOperation5);
		mapList.put("chart", findByOperation6);
		
		mapList.put("otherC", otherC);
		mapList.put("projectX", projectX);
		
		mapList.put("Mao", Mao);
		mapList.put("maoL", maoL);
		
		mapList.put("sjzc", sjzc);
		mapList.put("sjzcV", sjzcV);
		
		
		mapList.put("type", type);
		mapList.put("acount", acount);
		return mapList;
	}
	public static void main(String[] args) {
		
		List<String> as=Lists.newArrayList();
		as.add(11, "123");
	}
	 public static double formatDouble1(double d) {
	        return (double)Math.round(d*100)/100;
	    }
}
